//
//  CusutomNaviBar.m
//  LoginDemo
//
//  Created by XFB on 2017/6/13.
//  Copyright © 2017年 ZC. All rights reserved.
//

#import "CusutomNaviBar.h"


@interface CusutomNaviBar ()

@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *rightButton;

@end

@implementation CusutomNaviBar

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0.0f, 0.0f, [[UIScreen mainScreen] bounds].size.width, 64.0f);
    }
    return self;
}

#pragma mark --- <Lazy loading>
- (UIButton *)leftButton
{
    if (_leftButton == nil) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftButton setImage:[UIImage imageNamed:_leftImage] forState:UIControlStateNormal];
        _leftButton.imageView.contentMode = UIViewContentModeCenter;
        _leftButton.frame = CGRectMake(15, 20, 64, 44);
        [_leftButton addTarget:self action:@selector(clickLeftButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

- (UIButton *)rightButton
{
    if (_rightButton == nil) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightButton setImage:[UIImage imageNamed:_rightImage] forState:UIControlStateNormal];
        _rightButton.imageView.contentMode = UIViewContentModeCenter;
        _rightButton.frame = CGRectMake(ScreenWidth-15-44, 20, 44, 44);
        [_rightButton addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];

    }
    return _rightButton;
}

- (UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = _title;
        _titleLabel.textColor = _titleColor;
        _titleLabel.center = CGPointMake(self.frame.size.width/2, (self.frame.size.height+20.0f)/2);
        _titleLabel.bounds = CGRectMake(0.0f, 0.0f, 100.0f, self.frame.size.height-20.0f);
    }
    return _titleLabel;
}

#pragma mark --- <Setter And Getter>

- (void)setLeftImage:(NSString *)leftImage
{
    _leftImage = leftImage;
    [self.leftButton setImage:[UIImage imageNamed:leftImage] forState:UIControlStateNormal];
}

- (void)setRightImage:(NSString *)rightImage
{
    _rightImage = rightImage;
    [self.rightButton setImage:[UIImage imageNamed:_rightImage] forState:UIControlStateNormal];
}

- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLabel.text = title;
}

- (void)setTitleColor:(UIColor *)titleColor
{
    _titleColor = titleColor;
    self.titleLabel.textColor = titleColor;
}

- (void)layoutSubviews
{
    [self addSubview:self.leftButton];
    [self addSubview:self.titleLabel];
    [self addSubview:self.rightButton];
}

#pragma mark --- <Events Response>
/// 自定义导航栏左侧按钮点击
- (void)clickLeftButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(customNaviBarLeftButtonClick)]) {
        [self.delegate customNaviBarLeftButtonClick];
    }
}

/// 自定义导航栏右侧按钮点击
- (void)clickRightButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(customNaviBarRightButtonClick)]) {
        [self.delegate customNaviBarRightButtonClick];
    }
}


@end
